package org.apache.struts2.plugin.json.reflection;

import org.apache.struts2.plugin.json.model.JSONObject;
import static org.apache.struts2.plugin.json.reflection.JSONArrayReflection.*;

import org.apache.struts2.plugin.json.model.JSONValue;

/**
 * @author VERDOÏA Laurent <verdoialaurent@gmail.com>
 */
public abstract class Reflection {

    public static JSONValue reflection(Object o) throws JSONReflectionException {
        if (o == null) {
            return new JSONObject();
        }

        JSONValue value = null;

        value = JSONBooleanReflection.reflection(o);
        if (value != null) {
            return value;
        }

        value = JSONStringReflection.reflection(o);
        if (value != null) {
            return value;
        }

        value = JSONNumberReflection.reflection(o);
        if (value != null) {
            return value;
        }

        value = JSONArrayReflection.reflection(o);
        if (value != null) {
            return value;
        }

        value = JSONObjectReflection.reflection(o);
        if (value == null) {
            throw new JSONReflectionException("Can't reflection your object, please make a bug report.");
        }
        return value;
    }
}
